<template>
  <div class="app-container">

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['pms:productAttribute:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['pms:productAttribute:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['pms:productAttribute:remove']">删除</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="productAttributeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <!-- <el-table-column label="属性分类ID" align="center" prop="attributeCategoryId" /> -->
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="属性选择类型" align="center" prop="selectType" :formatter="selectTypeFormat" />
      <el-table-column label="属性录入方式" align="center" prop="inputType" :formatter="inputTypeFormat" />
      <el-table-column label="可选值列表" align="center" prop="inputList" />
      <el-table-column label="是否支持手动新增" align="center" prop="handAddStatus" :formatter="handAddStatusFormat" />
      <el-table-column label="属性的类型" align="center" prop="type" :formatter="typeFormat" />
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['pms:productAttribute:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['pms:productAttribute:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改产品属性对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="属性选择类型">
          <el-radio-group v-model="form.selectType">
            <el-radio v-for="dict in selectTypeOptions" :key="dict.dictValue" :label="parseInt(dict.dictValue)">{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="属性录入方式">
          <el-radio-group v-model="form.inputType">
            <el-radio v-for="dict in inputTypeOptions" :key="dict.dictValue" :label="parseInt(dict.dictValue)">{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="可选值列表" prop="inputList">
          <el-input v-model="inputListFormat" placeholder="请输入可选值列表" type="textarea" />
        </el-form-item>
        <el-form-item label="是否支持手动新增">
          <el-radio-group v-model="form.handAddStatus">
            <el-radio v-for="dict in handAddStatusOptions" :key="dict.dictValue" :label="parseInt(dict.dictValue)">{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="排序" prop="sort">
          <el-input-number v-model="form.sort" :step="1"></el-input-number>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio v-for="dict in statusOptions" :key="dict.dictValue" :label="parseInt(dict.dictValue)">{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listProductAttribute,
  getProductAttribute,
  delProductAttribute,
  addProductAttribute,
  updateProductAttribute,
  exportProductAttribute,
} from "@/api/pms/productAttribute";

export default {
  name: "ProductAttribute",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品属性表格数据
      productAttributeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 属性选择类型(0 唯一 1 单选 2 多选)字典
      selectTypeOptions: [],
      // 属性录入方式(0手工录入 1从列表中选取)字典
      inputTypeOptions: [],
      // 是否支持手动新增(0不支持 1支持)字典
      handAddStatusOptions: [],
      // 属性的类型(0规格 1参数)字典
      typeOptions: [],
      // 状态(0正常1停用)字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      inputListFormat: null,
    };
  },
  created() {
    this.queryParams.attributeCategoryId = this.$route.query.categoryId;
    this.queryParams.type = this.$route.query.type;

    this.getList();
    this.getDicts("pms_attr_select_type").then((response) => {
      this.selectTypeOptions = response.data;
    });
    this.getDicts("pms_attr_input_type").then((response) => {
      this.inputTypeOptions = response.data;
    });
    this.getDicts("pms_attr_hand_status").then((response) => {
      this.handAddStatusOptions = response.data;
    });
    this.getDicts("pms_attr_type").then((response) => {
      this.typeOptions = response.data;
    });
    this.getDicts("pms_data_status").then((response) => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    /** 查询产品属性列表 */
    getList() {
      this.loading = true;
      listProductAttribute(this.queryParams).then((response) => {
        this.productAttributeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 属性选择类型(0 唯一 1 单选 2 多选)字典翻译
    selectTypeFormat(row, column) {
      return this.selectDictLabel(this.selectTypeOptions, row.selectType);
    },
    // 属性录入方式(0手工录入 1从列表中选取)字典翻译
    inputTypeFormat(row, column) {
      return this.selectDictLabel(this.inputTypeOptions, row.inputType);
    },
    // 是否支持手动新增(0不支持 1支持)字典翻译
    handAddStatusFormat(row, column) {
      return this.selectDictLabel(this.handAddStatusOptions, row.handAddStatus);
    },
    // 属性的类型(0规格 1参数)字典翻译
    typeFormat(row, column) {
      return this.selectDictLabel(this.typeOptions, row.type);
    },
    // 状态(0正常1停用)字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        attributeCategoryId: this.$route.query.categoryId,
        name: null,
        selectType: 0,
        inputType: 0,
        inputList: null,
        handAddStatus: 0,
        type: this.$route.query.type,
        sort: null,
        isDel: null,
        status: 0,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加产品属性";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getProductAttribute(id).then((response) => {
        this.form = response.data;
        this.inputListFormat = response.data.inputList.replace(/,/g,'\n');
        this.open = true;
        this.title = "修改产品属性";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateProductAttribute(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProductAttribute(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除产品属性编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delProductAttribute(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有产品属性数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportProductAttribute(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
  },
  watch: {
    inputListFormat: function (newValue, oldValue) {
      newValue = newValue.replace(/\n/g, ",");
      this.form.inputList = newValue;
    },
  },
};
</script>
